﻿using System;
using System.Collections.Generic;
using System.Linq;
using BUS;
using System.Collections;

public partial class Search : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Page.Title = "Tìm kiếm sản phẩm";
            LoadCategoryProducts();
            LoadPriceFrom();
            LoadPriceTo();

            if (Request.QueryString["p"] != null)
            {
                txtKeySearch.Text = Request.QueryString["p"];
                SearchProducts();
            }
            else
                ViewState["listResult"] = null;
        }
    }

    //Load dropdownlist giá từ
    private void LoadPriceFrom()
    {
        var listPriceForm = new SortedList { { 0, "---Giá Từ (VND)---" } };
        for (var i = 5; i <= 20; i += 5)
        {
            listPriceForm.Add(i, String.Format("{0:0,0 triệu}", i * 100));
        }

        ddlPriceFrom.DataSource = listPriceForm;
        ddlPriceFrom.DataTextField = "value";
        ddlPriceFrom.DataValueField = "key";
        ddlPriceFrom.DataBind();
    }

    //Load dropdownlist giá đến, nhận giá trị từ ddlPriceFrom...
    private void LoadPriceTo()
    {
        var posEnd = Int32.Parse(ddlPriceFrom.SelectedValue);

        var listPriceForm = new SortedList { { -1, "---Giá đến (VND)---" } };

        if (posEnd != 0)
        {
            for (var i = posEnd; i <= 52; i += 5)
            {
                listPriceForm.Add(i, String.Format("{0:0,0 triệu}", i * 100));
            }
            //i -= 4;
            //listPriceForm.Add(100, String.Format("Trên {0:0,0 }", i * 1000000));
        }

        ddlPriceTo.DataSource = listPriceForm;
        ddlPriceTo.DataTextField = "value";
        ddlPriceTo.DataValueField = "key";
        ddlPriceTo.DataBind();
    }
    //load dropdownlist Loại sản phẩm
    private void LoadCategoryProducts()
    {
        var list = LoaiSanPhamBUS.GetAll();
        LoaiSanPhamBUS none = new LoaiSanPhamBUS { ID = 0, TenLoai = "--Chọn loại sản phẩm--" };
        list.Insert(0, none);

        ddlCatePros.DataSource = list;
        ddlCatePros.DataTextField = "TenLoai";
        ddlCatePros.DataValueField = "ID";
        ddlCatePros.DataBind();
    }

    protected void ddlPriceFrom_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadPriceTo();
    }

    protected void btnSumit_Click(object sender, EventArgs e)
    {
        SearchProducts();
    }

    private void SearchProducts()
    {
        SearchBUS srh = new SearchBUS()
        {
            KeyWord = txtKeySearch.Text.Trim(),
            IDCategoryProduct = Int32.Parse(ddlCatePros.SelectedValue),
            PriceFrom = Int32.Parse(ddlPriceFrom.SelectedValue),
            PriceTo = Int32.Parse(ddlPriceTo.SelectedValue)
        };

        MultiView1.SetActiveView(ViewResultSearch);
        lblResult.Text = srh.KeyWord + " - " + srh.IDCategoryProduct + " - " + srh.PriceFrom + " - " + srh.PriceTo;

        ViewState["listResult"] = srh;


    }

    protected void DataPager1_PreRender(object sender, EventArgs e)
    {
        if (ViewState["listResult"] != null)
        {
            SearchBUS srh = (SearchBUS)ViewState["listResult"];
            var list = srh.Search();
            lvResultSearch.DataSource = list;
            lvResultSearch.DataBind();

            lblResult.Text = "tìm thấy <span style='color:red'>" + list.Count() + "</span> sản phẩm";
        }
    }
}
